package com.xbs.dao.hitchhiking;
/**
 * 顺风车dao
 * @author dell
 */

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xbs.dao.conn.BaseDao;
import com.xbs.dao.conn.ParamSet;
import com.xbs.dao.conn.RowDatas;
import com.xbs.entity.CarModelEntity;
import com.xbs.entity.CityEntity;
import com.xbs.entity.HitchEntity;
import com.xbs.entity.HitchhikeEntity;
import com.xbs.entity.StoreEntity;

public class HitchDao extends BaseDao{

	private RowDatas<HitchEntity> hitchentity;//创建一个对象
	private RowDatas<CityEntity> city;//声明一个城市对象结果处理对象
	private RowDatas<CarModelEntity> carmodel;//声明一个车品牌对象
	private RowDatas<StoreEntity> store;//声明一个门店对象
	
	public HitchDao(){
		hitchentity = new HitchData();
		city = new CitySomeData();
		carmodel = new CarTypeData();
		store = new StoreData();
	}
	
	/**
	 * 
	 * 1.查询所显示城市	 
	 */
	public List<CityEntity> getSomeCity(String cityname){
		String sql = "select * from tb_city where city_name in ("+cityname+")"; 
		return queryList(sql, city);
	}
	
	/**
	 * 2.查询车的品牌
	 */
	public List<CarModelEntity> getHitchByType(){
		String sql = "select car_model_name from tb_car_model ";
		return queryList(sql,carmodel);	
	}
	
	/**
	 *3. 根据城市，车型，取车时间不同的条件查询顺风车
	 * 先查询所有的信息
	 */
	public List<HitchEntity> getHitch(String select){
		String sql ="select c.car_type,c.car_model_id,hc.hitch_id,date_format(hc.hitch_time,'%Y-%m-%d') as hitch_time,"
				+ " date_format(hc.hitch_return_time,'%Y-%m-%d') as return_time, "
				+ " hc.hitch_day,hc.hitch_money,hc.hitch_miles,cm.car_model_name,cm.car_model_seating,"
				+ " cm.car_model_image,s.store_name,s.store_city,s2.store_name as store_name1,"
				+ " s2.store_city as store_city1 from tb_car c "
				+ " left join tb_car_model cm on c.car_model_id= cm.car_model_id "
				+ " right join tb_hitchhiking hc on hc.hitch_id = c.car_id "
				+ " left join tb_store s on s.store_id = hc.hitch_num "
				+ " left join tb_store s2 on s2.store_id = hc.hitch_return_num"
				+ " where 1=1"+select;
		
		return queryList(sql,hitchentity);
	}
	
	/**
	 * 4.根据门店id查询门店名称
	 */
	public StoreEntity getCityByStoreid(int doorid){
		String sql = "select store_name,store_city FROM tb_store "
				+ " where tb_store.store_id = ?";
		return query(sql,new ParamSet(doorid),store);
	}
	
	
	
	/**
	 * 城市名称:通过内部类实现接口处理，内部类仅供本类使用
	 * @author dell
	 */
	private class CitySomeData implements RowDatas<CityEntity>{

		@Override
		public CityEntity getRowData(ResultSet rs) throws SQLException {
			CityEntity city = new CityEntity();
			city.setCity_id(rs.getInt("city_id")); 
			city.setCity_name(rs.getString("city_name"));
			city.setParent_id(rs.getInt("parent_id"));
			return city;
		}
		
	}
	
	public class HitchData implements RowDatas<HitchEntity>{

		@Override
		public HitchEntity getRowData(ResultSet rs) throws SQLException {
			HitchEntity hitchbean = new HitchEntity();
			hitchbean.setHitch_day(rs.getInt("hitch_day"));
			hitchbean.setHitch_id(rs.getString("hitch_id"));
			hitchbean.setHitch_money(rs.getDouble("hitch_money"));
		/*	hitchbean.setHitch_num(rs.getInt("hitch_num"));
			hitchbean.setHitch_return_num(rs.getInt("hitch_return_num"));
		    hitchbean.setHitch_status(rs.getInt("hitch_status"));
		    hitchbean.setStore_id(rs.getInt("store_id"));*/
			hitchbean.setHitch_return_time(rs.getDate("return_time"));
			hitchbean.setHitch_time(rs.getDate("hitch_time"));
			hitchbean.setHitch_miles(rs.getDouble("hitch_miles"));
			hitchbean.setStart_store_city(rs.getString("store_city"));
			hitchbean.setEnd_store_city(rs.getString("store_city1"));
			hitchbean.setStart_store_name(rs.getString("store_name"));
			hitchbean.setEnd_store_name(rs.getString("store_name1"));
			hitchbean.setCarmodelimage(rs.getString("car_model_image"));
			hitchbean.setCarmodelname(rs.getString("car_model_name"));
			hitchbean.setSeating(rs.getInt("car_model_seating"));
			hitchbean.setCar_type(rs.getString("car_type"));
		/*  hitchbean.setStore_person(rs.getString("store_person"));
			hitchbean.setStore_remark(rs.getString("store_remark"));
			hitchbean.setCarmodelgps(rs.getString("car_model_gps"));*/
			hitchbean.setCarmodelid(rs.getString("car_model_id"));
		/*  hitchbean.setCarmodelseat(rs.getString("car_model_seat"));
			hitchbean.setCarmodeltype(rs.getString("car_model_type"));
			hitchbean.setCarmodelyear(rs.getString("car_model_year"));
			hitchbean.setDiscount(rs.getDouble("company_discount"));
			hitchbean.setDoornumber(rs.getInt("car_model_door_number"));
			hitchbean.setFueltype(rs.getString("car_model_fuel_type"));
			hitchbean.setCar_id(rs.getString("car_id"));
			hitchbean.setCar_model_id(rs.getInt("car_model_id"));
			hitchbean.setCar_store_id(rs.getInt("store_id"));*/
			
			
			return hitchbean;
		}
		
	}
	
	
	/**
	 *查询车型品牌
	 */
	private class CarTypeData implements RowDatas<CarModelEntity>{

		@Override
		public CarModelEntity getRowData(ResultSet rs) throws SQLException {
			CarModelEntity cme = new CarModelEntity();
			cme.setCarmodelname(rs.getString("car_model_name"));
			return cme;
		}
		
	}
	
	/**
	 * 查询门店表
	 */
	private class StoreData implements RowDatas<StoreEntity>{

		@Override
		public StoreEntity getRowData(ResultSet rs) throws SQLException {
		StoreEntity store = new StoreEntity();
		store.setStore_city(rs.getString("store_city"));
		store.setStore_name(rs.getString("store_name"));
			return store;
		}
		
	}
}
